odoo.define('pdf_test.many2many_binary_include', function (require) {

    "use strict";
    var many2many_binary = require('web.relational_fields').FieldMany2ManyBinaryMultiFiles;
    var DocumentViewer = require('mail.DocumentViewer');
    var fieldRegistry = require('web.field_registry');

    var many2many_binary_preview = many2many_binary.extend({

        events: _.extend({}, many2many_binary.prototype.events, {
            'click .preview': 'preview_click',
        }),
        cssLibs: [
            '/sys_tools/static/src/css/many2many_binary_preview.css',
        ],
        _render: function () {
            this._super.apply(this, arguments);
            this.$('.o_image[data-mimetype^="application/pdf"]').each(function () {
                var $pdf = $(this);
                if ($pdf.data('mimetype') === 'application/pdf') {
                    $pdf.parent().removeAttr('href')
                    $pdf.parent().addClass('preview')
                    $(this).css("cursor", "zoom-in")
                }
            });
            this.$('.o_image[data-mimetype^="image"]').each(function () {
                var $img = $(this);
                $img.parent().removeAttr('href')
                $img.parent().addClass('preview')
                $img.css('background-image', "url('/web/image/"+$img.parent().parent().attr('data-id')+"/90x90/?crop=true')");
                $(this).css("cursor", "zoom-in")
            });
            this.$('.o_image[data-mimetype^="text/plain"]').each(function () {
                var $txt = $(this);
                $txt.parent().removeAttr('href')
                $txt.parent().addClass('preview')
                $(this).css("cursor", "zoom-in")
            });
            this.$('.o_image[data-mimetype^="video"]').each(function () {
                var $video = $(this);
                $video.parent().removeAttr('href')
                $video.parent().addClass('preview')
                $(this).css("cursor", "zoom-in")
            });

        },
        preview_click: function (event) {
            // debugger
            var self = this;
            var t_id = $(event.currentTarget).parent().attr('data-id')
            var t_name = $(event.currentTarget).parent().parent().parent().attr('title')
            var attachments = [{
                "filename": t_name,
                "id": t_id,
                "is_main": true,
                "mimetype": $(event.target).attr('data-mimetype'),
                "name": self.t_name,
                "data_ext": $(event.target).attr('data-ext'),
            }];
            var attachmentViewer = new DocumentViewer(self, attachments, t_id);
            attachmentViewer.appendTo($('body'));
        },
    })

    fieldRegistry.add('many2many_binary_preview', many2many_binary_preview);
})
